package com.qf.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.dao.CheckGroupDao;
import com.qf.entity.PageResult;
import com.qf.entity.QueryPageBean;
import com.qf.pojo.CheckGroup;
import com.qf.pojo.CheckItem;
import com.qf.service.CheckGroupService;
import com.qf.service.CheckItemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

/**
 * @author zhouhao
 * @create 2021-12-22 11:35
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {

    @Autowired
    private CheckGroupDao checkGroupDao;

    //新增检查组，同时需要让检查组关联检查项
    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        //新增检查组，操作t_checkgroup表
        checkGroupDao.add(checkGroup);
        //获取返回值ID
        Integer checkGroupId = checkGroup.getId();
        //设置检查组和检查项的多对多的关联关系，操作t_checkgroup_checkitem表
        for (Integer checkitemId : checkitemIds) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("checkgroupId", checkGroupId);
            map.put("checkitemId", checkitemId);
            checkGroupDao.addCheckGroupAndCheckItem(map);
        }
    }

    //分页查询检查组
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        //开启分页
        PageHelper.startPage(currentPage, pageSize);
        List<CheckGroup> checkGroupList = checkGroupDao.findPage(queryString);
        PageInfo<CheckGroup> pageInfo = new PageInfo<>(checkGroupList);
        long total = pageInfo.getTotal();
        List<CheckGroup> list = pageInfo.getList();
        return new PageResult(total, list);
    }

    //删除检查组
    @Override
    public void deleteById(Integer id) {
        //删除检查组所关联的检查项
        checkGroupDao.deleteCheckGroupAndCheckItem(id);
        //删除检查组
        checkGroupDao.deleteCheckGroupById(id);
    }

    //根据ID查找检查组
    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    //根据检查组ID查询检查组包含的多个检查项ID
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer id) {
        return checkGroupDao.findCheckItemIdsByCheckGroupId(id);
    }

    //更新检查组
    @Override
    public void edit(Integer[] checkitemIds, CheckGroup checkGroup) {
        //删除关联关系
        Integer checkGroupId = checkGroup.getId();
        checkGroupDao.deleteCheckGroupAndCheckItem(checkGroupId);
        //更新检查组
        checkGroupDao.updateCheckGroup(checkGroup);
        //建立检查组与检查项关联
        for (Integer checkitemId : checkitemIds ){
            HashMap<String, Object> map = new HashMap<>();
            map.put("checkgroupId",checkGroupId);
            map.put("checkitemId",checkitemId);
            checkGroupDao.addCheckGroupAndCheckItem(map);
        }
    }

    //查找所有检查组
    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }

}
